package com.kreedx.kdemo.module;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.alibaba.android.vlayout.Range;
import com.kreedx.kdemo.R;
import com.kreedx.kdemo.module.contract.TangramDemoContract;
import com.kreedx.kdemo.module.presenter.TangramDemoPresenter;
import com.kreedx.kdemo.view.DebugView;
import com.libra.Utils;
import com.tmall.wireless.tangram.TangramBuilder;
import com.tmall.wireless.tangram.TangramEngine;
import com.tmall.wireless.tangram.core.adapter.GroupBasicAdapter;
import com.tmall.wireless.tangram.dataparser.concrete.Card;
import com.tmall.wireless.tangram.structure.BaseCell;
import com.tmall.wireless.tangram.support.async.AsyncLoader;
import com.tmall.wireless.tangram.support.async.AsyncPageLoader;
import com.tmall.wireless.tangram.support.async.CardLoadSupport;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import io.xujiaji.xmvp.view.base.v4.XBaseFragment;

import static android.os.Looper.getMainLooper;


public class TangramDemoFragment extends XBaseFragment<TangramDemoPresenter> implements TangramDemoContract.View {

    public static TangramDemoFragment newInstance() {
        
        Bundle args = new Bundle();
        
        TangramDemoFragment fragment = new TangramDemoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    RecyclerView recyclerView;
    Handler mMainHandler;
    TangramEngine engine;
    @Override
    public void onInitCircle() {
        super.onInitCircle();
        recyclerView = getRootView().findViewById(R.id.recycler_view);

        mMainHandler = new Handler(getMainLooper());

        TangramBuilder.InnerBuilder builder = TangramBuilder.newInnerBuilder(getActivity());
        builder.registerCell("Debug", DebugView.class);
        engine = builder.build();

        engine.bindView(recyclerView);

        Utils.setUedScreenWidth(720);


        String json = new String(getAssertsFile(getActivity(), "data.json"));
        JSONArray data = null;
        try {
            data = new JSONArray(json);
            engine.setData(data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_tangram;
    }

    public static byte[] getAssertsFile(Context context, String fileName) {
        InputStream inputStream = null;
        AssetManager assetManager = context.getAssets();
        try {
            inputStream = assetManager.open(fileName);
            if (inputStream == null) {
                return null;
            }

            BufferedInputStream bis = null;
            int length;
            try {
                bis = new BufferedInputStream(inputStream);
                length = bis.available();
                byte[] data = new byte[length];
                bis.read(data);

                return data;
            } catch (IOException e) {

            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (Exception e) {

                    }
                }
            }

            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
